from pyglet.gl import *
import pyglet
from pyglet.window import *
from pyglet import image
import os
import math
"""
Port of the NeHe lessons to Pyglet.


@created: Oct, 2008
@author: Andriy Drozdyuk
"""

window = pyglet.window.Window(width=640, height=480, resizable=True)

xrot=yrot=zrot=0.0	# rotate the cube
points = []

wiggle_count = 0	# counter used to control how fast flag waves

def load_gl_textures():
	# load bitmaps and convert to textures
	global texture
	texture_file = os.path.join('data', 'tim.bmp')
	texture_surf = image.load(texture_file)
	texture = texture_surf.get_texture()
	glBindTexture(GL_TEXTURE_2D, texture.id)

	#glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
	#glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

@window.event
def on_key_press(symbol, modifiers):
  if symbol == key.RETURN or symbol==key.ESCAPE:
		exit()

def update(dt):
  	global xrot, yrot, zrot
	zrot+=0.5
	yrot+=0.4
	zrot+=0.3

	# Spin the objects with a constant speed

pyglet.clock.schedule(update)

@window.event
def on_resize(width, height):
	if height==0:
		height = 1
	glViewport(0, 0, width, height)

	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	# Calculate the aspect ratio of the window
	gluPerspective(45.0, 1.0*width/height, 0.1, 100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	return pyglet.event.EVENT_HANDLED

	
# For displaying fps
fps_display = pyglet.clock.ClockDisplay()

def init():
  	global points
  	"""
	Pyglet oftentimes calls this setup()	
	"""


	glEnable(GL_TEXTURE_2D)
	load_gl_textures()

  	glShadeModel(GL_SMOOTH)	# Enables smooth shading
	glClearColor(0.0, 0.0, 0.0, 0.0) #Black background
	
	glClearDepth(1.0)		# Depth buffer setup
	glEnable(GL_DEPTH_TEST)		# Enables depth testing
	glDepthFunc(GL_LEQUAL)		# The type of depth test to do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)	# Really nice perspective calculations

	glPolygonMode(GL_BACK, GL_FILL)	# back face is filled in
	glPolygonMode(GL_FRONT, GL_LINE)	# front face is drawn with lines

	# loop through x plane
	for x in range(45):
	  	# loop through y plane
		row = []
		for y in range(45):
		  	# Apply the wave to our mesh
			el = [ x/5.0 - 4.5, y/5.0 - 4.5,  math.sin(2.0*math.pi*(x/5.0)*40/360.0)]
			row.append(el)
		points.append(row)



@window.event
def on_draw():
  	global points, xrot, yrot, zrot, wiggle_count

	# show our fps
	fps_display.draw()

  	# Here we do all the drawing
	glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
	glLoadIdentity()	

	glTranslatef(0.0, 0.0, -12.0) # Move left 1.5 units and into the screen 6.0
	
	glRotatef(xrot, 1.0, 0.0, 0.0)
	glRotatef(yrot, 0.0, 1.0, 0.0)
	glRotatef(zrot, 0.0, 0.0, 1.0)

	red = (1.0, 0.0, 0.0)
	green = (0.0, 1.0, 0.0)
	blue = (0.0, 0.0, 1.0)
	orange = (1.0, 0.5, 0.0)
	yellow = (1.0, 1.0, 0.0)
	violet=(1.0, 0.0, 1.0)

	load_gl_textures()
	#glBindTexture(GL_TEXTURE_2D, texture.id)
	glBegin(GL_QUADS)
	for x in range(44):
	  	for y in range(44):
		  	f_x = x/44.0
			f_y = y/44.0
			f_xb = (x+1)/44.0
			f_yb = (y+1)/44.0

			# first texture coord bottom left
			glTexCoord2f(f_x, f_y)
			glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2])

			# second texture coord top left
			glTexCoord2f(f_x, f_yb)
			glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2])

			# third texture coord top right
			glTexCoord2f(f_xb, f_yb)
			glVertex3f(*points[x+1][y+1])

			# fouth texture coordinate bottom right
			glTexCoord2f(f_xb, f_y)
			glVertex3f( *points[x+1][y] )

	glEnd()

	# used to slow down the wave (every 2nd frame only_
	if wiggle_count == 2:
	  for y in range(45):
		val = points[0][y][2]
		for x in range(44):
		  # current wave value equals value to the right
		  points[x][y][2] = points[x+1][y][2]
		points[44][y][2] = val
	  wiggle_count = 0
	wiggle_count += 1

	xrot+=0.3
	yrot+=0.2
	zrot+=0.4


	return pyglet.event.EVENT_HANDLED
  	
def m3(matrix):
  for row in matrix:
	v3(*row)
	
def v3(x,y,z):
  glVertex3f( float(x), float(y), float(z))

init()	

pyglet.app.run()
